Given an integer array queries
and a positive integer intLength
, return an arrayanswer
whereanswer[i]
is either thequeries[i]th
smallest positive palindrome of lengthintLength
or-1
if no such palindrome exists.
A palindrome is a number that reads the same backwards and forwards. Palindromes cannot have leading zeros.
Input: queries = [1,2,3,4,5,90], intLength = 3 Output: [101,111,121,131,141,999] Explanation: The first few palindromes of length 3 are: 101, 111, 121, 131, 141, 151, 161, 171, 181, 191, 202, ... The 90th palindrome of length 3 is 999.
Input: queries = [2,4,6], intLength = 4 Output: [1111,1331,1551] Explanation: The first six palindromes of length 4 are: 1001, 1111, 1221, 1331, 1441, and 1551.
1 <= queries.length <= 5 * 104
1 <= queries[i] <= 109
1 <= intLength <= 15
implSolution{pubfnkth_palindrome(queries:Vec<i32>,int_length:i32) -> Vec<i64>{letmut ret = Vec::with_capacity(queries.len());for&query in&queries {let half_length = (int_length asu32 + 1) / 2;if query >= 9*10_i32.pow(half_length - 1) + 1{ ret.push(-1);continue;}letmut x = query asi64 + 10_i64.pow(half_length - 1) - 1;letmut y = x;if int_length % 2 == 1{ y /= 10;}for _ in0..int_length / 2{ x = x *10 + y % 10; y /= 10;} ret.push(x);} ret }}